/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : VerticalSliderInfoTag.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Oct 11, 2011  mycodetalks			       Created
 */
package net.lwf.web.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.common.VerticalSliderInfo;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.number.util.RandomNumberGenerator;

public class VerticalSliderInfoTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	
	private String separator_asterisk = " ** ";
	private String separator_hash = " ## ";
	private int DISPLAY_ITEMS = 5;
	
	private String separator = separator_asterisk;
	private String subject;
	
	public int doEndTag() throws JspException {

		try {
			pageContext.getOut().println(this.getDisplayInfo());
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
			return EVAL_PAGE;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	private String getDisplayInfo() {
		int loops = DISPLAY_ITEMS;
		String display = ICommonConstant.EMPTY;
		String[] displayInfo = VerticalSliderInfo.getInstance().getMapInfo().get(subject);
		if(displayInfo.length < loops) {
			loops = displayInfo.length;
		}
		int[] uniques = RandomNumberGenerator.generateInts(1, displayInfo.length, loops);
		int random = RandomNumberGenerator.generate(1, 20);
	
		if(random%2 == 0) {
			separator = separator_hash;
		}
		
		for(int i=0; i<loops; i++) {
			display += displayInfo[uniques[i]-1];
			if(i+1<loops) {
				display += separator;
			}
		}
		
		return display;
	}
	
	

}
